/*************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/


/**
 * @file
 * crtmpprotocol.h
 *
 * @author
 * Ales Podskalsky (<a href="mailto:podskale@fel.cvut.cz">podskale@fel.cvut.cz</a>)
 *
 * @brief
 * Implements interface for work RTMP.
 *
 * @todo
 * Copy useful functions from CRtmpSession class.
 */

#ifndef CRTMPPROTOCOL_H
#define CRTMPPROTOCOL_H


#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <fcntl.h>

#include <map>

#include "../Socket/Socket.h"
#include "crtmpprotocol.h"
#include "camf0stream.h"
#include "crtmpchunk.h"
#include "crtmpmessage.h"
#include "crtmpmsgcreator.h"

class CRtmpProtocol
{
    public:
        /**
         * @brief
         * Default contructor.
        */
        CRtmpProtocol(int iSocket);

        /**
         * @brief
         * Destructor.
        */
        virtual ~CRtmpProtocol();

    public:
        /**
         * @brief
         * Function for do handshake over RTMP protocol.
         *
         * @return
         * Boolean value shows result of the handshake.
        */
        bool Handshake();

        bool GetNextMessage(CRtmpMessage &rtmpMessage);
        bool SendMessage(const CRtmpMessage &rtmpMessage, int iCsID = 3, int type = 0);
        bool IsAlive() const;

       // CRtmpMessage OnConnectReply();

    private:
        /**
         * @brief
         * Function creates packet from RTMP message.
         *
         * @return
         * std::string as payload for CPacket.
        */
        std::string CreatePacket(CRtmpMessage message, int iCsID = 3, int type = 0);

        /**
         * @brief
         * Function for read and parse Chunk from input buffer.
         *
         * @return
         * CRtmpChunk object represents RTMP chunk.
        */
        CRtmpChunk ReadChunk();


        /**
         * @brief
         * Ancillary function for writing CPacket to the output buffer.
         *
         * @param packet
         * Packet which we want to write to the buffer.
         *
         * @throw CRtmpException
         * Throws exception if data in the packet can't be sent.
         *
         * @return
         * Integer value shows how many bytes was realy written.
        */
        int Write(CPacket packet) const throw (CRtmpException &);

        /**
         * @brief
         * Ancillary function for reading CPacket from the intput buffer.
         *
         * @param iSize
         * Number of bytes we want to read from the input buffer.
         *
         * @throw CRtmpException
         * Throws exception if data can't be read from the input buffer.
         *
         * @return
         * CPacket object read from the input buffer.
        */
        CPacket Read(int iSize) const throw (CRtmpException &);

        /**
         * @brief
         * Function for checking if something is in the input tcp stack
         *
         * @throw CRtmpException
         * Throws exception if connection is lost or other error (select returns -1).
         *
         * @return
         * Retrurs true if buffer something contains else returns false.
        */
        bool CheckBuffer() const throw (CRtmpException &);

    private:
        int m_iSocket;
        bool m_bConnectionAlive;

        int m_iChunkSize;
        std::map<int, CRtmpMessage> m_mapChunkStreams;
};

#endif // CRTMPPROTOCOL_H
